<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;

/**
 * 手牌商品设置控制器
 * @package app\index\controller
 */
class HandGoods extends Auth
{
    public $noNeedLogin = [];

    /**
     *获取优先排钟列表
     * @return array
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $page = $this->request->post('page')? '' : 1;
            $limit = $this->request->post('page')? '' : 10;
            $data = Db::name('qt_hand_goods')
                ->field('g.id,g.type,g.number,gg.name')
                ->alias('g')
                ->leftJoin('qt_goods gg','g.product_id = gg.id')
                ->where(['g.store_id' => $userInfo['store_id']])
                ->order('g.id desc')
                ->paginate($limit);
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 添加手牌商品
     */
    public function add(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['type']) || empty($getData['product_id']) || empty($getData['number'])){
                $this->result('缺少参数', '', 204);
            }
            $getData['store_id'] = $this->userInfo['store_id'];
            $insert = Db::name('qt_hand_goods')->insert($getData);
            if($insert){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 编辑手牌商品
     */
    public function save(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['type']) || empty($getData['product_id']) || empty($getData['number']) || empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $update= Db::name('qt_hand_goods')->where('id',$getData['id'])->update($getData);
            if($update !== false){
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 编辑手牌商品
     */
    public function edit(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data = Db::name('qt_hand_goods')
                ->field('g.*,gg.name')
                ->alias('g')
                ->leftJoin('qt_goods gg','g.product_id = gg.id')
                ->where(['g.id' => $getData['id']])
                ->find();
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }



    /**
     * 删除手牌商品
     */
    public function delete(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $del = Db::name('qt_hand_goods')->where('id',$getData['id'])->delete();
            if($del){
                $this->result('删除成功', '', 200);
            }
            $this->result('删除失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


}
